package com.fishingwithme.infrastructure;

import com.fishingwithme.infrastructure.utils.CommonUtils;
import com.fishingwithme.infrastructure.utils.IdGenerator;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AuditorByAutoFillHandler implements MetaObjectHandler {
    @Autowired
    private IdGenerator idGenerator;

    @Override
    public void insertFill(MetaObject metaObject) {

        Long userId = CommonUtils.getUserId();
        Long id = idGenerator.nextId();
        this.strictInsertFill(metaObject, "id", Long.class, id);
        this.strictInsertFill(metaObject, "createdBy", Long.class, userId);
        this.strictInsertFill(metaObject, "createdAt", Date.class, new Date());
        this.strictInsertFill(metaObject, "updatedBy", Long.class, userId);
        this.strictInsertFill(metaObject, "updatedAt", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = CommonUtils.getUserId();
        this.strictUpdateFill(metaObject, "updatedBy", Long.class, userId);
        this.strictUpdateFill(metaObject, "updatedAt", Date.class, new Date());

    }

}
